﻿using UnityEngine;
using Util;
/// <summary>
/// 单例的基础类，实现单例模式的类可以继承与它
/// </summary>
public class Singleton<T> where T : class, new()
{
    private static T _instance = null;

    public static T Instance
    {
        get
        {
            if(_instance == null)
            {
                _instance = new T();
            }
            return _instance;
        }
    }

    public void SelfDispose()
    {
        _instance = null;
    }
}

public class SingletonScene<T> :MonoBehaviour where T : MonoBehaviour
{
    private static T _instance = null;

    public static T Instance
    {
        get
        {
            if(_instance == null)
            {
                GameObject go = new GameObject(typeof(T).ToString());
                _instance = go.AddComponent<T>();
                DontDestroyOnLoad(go);
            }
            return _instance;
        }
    }

    public void SelfDispose()
    {
        GameObject.Destroy(gameObject);
        _instance = null;
    }
}